package com.aaa.java12;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/7/15 14:35
 * @description：学生类
 * @modified By：
 * @version: 1.0
 */
public class Student  implements  Comparable<Student>, Serializable {
    private static final long serialVersionUID = 7343834897563120407L;
    private  int id;
    private String name;
    private int age;
    private Date birthday;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }

    public static void main(String[] args) {
        Student[] students= new Student[3];
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Student student1= new Student();
        student1.setId(100);
        student1.setAge(30);
        student1.setName("张三");
        String date1="1992-12-12";
        Date date = null;
        try {
            date = simpleDateFormat.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        student1.setBirthday(date);
        students[0]=student1;



        Student student2= new Student();
        student2.setId(101);
        student2.setAge(40);
        student2.setName("张三1");
        String date2="1982-12-12";
        Date date22 = null;
        try {
            date22 = simpleDateFormat.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        student2.setBirthday(date22);
        students[1]=student2;



        Student student3= new Student();
        student3.setId(103);
        student3.setAge(38);
        student3.setName("张三3");
        String date3="1984-12-12";
        Date date33 = null;
        try {
            date33 = simpleDateFormat.parse(date3);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        student3.setBirthday(date33);
        students[2]=student3;

        System.out.println("排序之前");
        for (Student student : students) {
            System.out.println(student);
        }

        for (int i = 1; i <students.length ; i++) {
            for (int j = 0; j < students.length - i; j++) {
                if(students[j].getAge()>students[j+1].getAge()){
                    Student temp=new Student();
                    temp= students[j];
                    students[j]=students[j+1];
                    students[j+1]=temp;

                }
            }
        }
        System.out.println("排序之后");
        for (Student student : students) {
            System.out.println(student.toString());
        }
    }

    @Override
    public int compareTo(Student o) {
        if(this.getAge()>o.getAge()){
            return 1;
        }
        if(this.getAge()<o.getAge()){
            return -1;
        }
        return 0;
    }
}
